<?php         
$db = new Database;
$auth = Auth::getAuth('current_user');
$user_id = $auth['id'];
$company_id = $auth['company_id'];
?>
<div class="row-fluid" >
<div class="span12" style="padding: 5px;min-height: 450px;background-color: #FFF;border: 1px solid #ddd; overflow: auto;">
    <legend class="userManagement" id="announcement_setting" style="cursor: pointer" rel="announcement_setting"><h5>Announcement Settings <img src="/images/flowchart/right_arrow.png" height="20" style="float: right" /></h5></legend>
        <div class="announcement_setting" style="display: none">
            <div class="span5 pull-left" style="padding-right: 30px;border-right: 1px solid #CCC">
               <h6>Announcement List</h6>
               <?php
                $queryAnnounList = $db->query("SELECT * from tbannouncement WHERE company_id = '$company_id' AND is_active='1'","array");
                $numrowsAnnounList = $db->query("SELECT * from tbannouncement WHERE company_id = '$company_id' AND is_active='1'","numrows");
               ?>
               <table class="table_data">
                    <thead>
                        <tr>
                            <th>#</th>
                            <th>Annoncement</th>
                            <th>Status</th>
                            <th colspan="3">Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        if($numrowsAnnounList==0){
                        ?>
                            <tr>
                                <td colspan="6" style="text-align:center">No Record Found</td>
                            </tr>
                        <?php
                        }else{
                            foreach($queryAnnounList as $dataAnnounList){
                                ?>
                                <tr>
                                    <td style="text-align:center" width="30"><?php echo $dataAnnounList['id']; ?></td>
                                    <td style="text-align:center" width="300">
                                        <?php
                                            $announcement = $dataAnnounList['announcement'];
                                            $post = substr($announcement, 0, 20);
                                            $announcementLength = strlen($announcement);
                                            echo "<div class='viewAnnouncement' />";
                                            echo $post . "..." ."<br /><a id='loadMore' data-announcement-length='$announcementLength' data-announcement='$announcement'>View More</a>";
                                            echo "</div>";
                                        ?>
                                    </td>
                                    <td style="text-align:center"  width="50"><?php echo status($dataAnnounList['is_active']); ?></td>
                                        <?php
                                            echo statCol($dataAnnounList['is_active'],$dataAnnounList['id'],"announcement","");
                                        ?>
                                </tr>
                                <?php    
                            }
                        }
                        ?>
                    </tbody>
                </table>
            </div>
            <script>
                // Plugin for this Administer Module
                administerModule = {
                    // Announcement
                    viewMore : function(element){
                        $(element).live("click", function(){
                            announcementLength = $(this).attr("data-announcement-length");
                            announcement = $(this).attr("data-announcement");
                            viewAllAnnouncement = announcement.substr(0, announcementLength);
                            $(".viewAnnouncement").html(viewAllAnnouncement + "\n" + "<a id='lessMore' data-announcement-length='"+announcementLength+"' data-announcement='"+announcement+"'>Less More</a>");
                        });
                    },
                    lessMore : function(element){
                        $(element).live("click", function(){
                            announcementLength = $(this).attr("data-announcement-length");
                            announcement = $(this).attr("data-announcement");
                            viewAllAnnouncement = announcement.substr(0, 20);
                            $(".viewAnnouncement").html(viewAllAnnouncement + "...\n" + "<br /><a id='loadMore' data-announcement-length='"+announcementLength+"' data-announcement='"+announcement+"'>View More</a>");
                        });
                    },
                    /*
                    saveNewAnnouncement : function(element){
                        $(element).live("click",function(){
                            announcement = $(".announcement").val();
                            $.post("/ajax/actionAdmin",{action:"updateAnnouncement",announcement:announcement},function(data){
                                showNotification({
                                    message: "Announcement Updated",
                                        type: "success",
                                   autoClose: true,
                                   duration: 3
                                });
                                $("html").append('<meta http-equiv="refresh" content="2">'); //for refresh
                            })
                        })
                    },*/
                    // View Settings
                    
                    appendNumCols : function(element){
                        a = 0;
                        $(element).live("click", function(){
                            numCols = $("#numCols").val(); // input number of columns you want
                            // Show created cols according to the inputed number of columns
                            if(numCols==0){
                                showNotification({
                                    message: "Please input the number of column that you want to create.",
                                    type: "error",
                                    autoClose: true,
                                    duration: 3
                                });
                            }else{
                                if(a==0){
                                    for (var n = 1; n <= numCols; ++ n){
                                        $(".showCreatedCols").append(
                                        'Title of Column '+n+': * <br />',
                                        '<input type="text" name="setofCols[]" id="setofCols" class="input-small" />  <br />');
                                    }
                                    $('<input type="button" class="btn" value="Go" id="saveSetofCols"/>').insertAfter(".showCreatedCols");
                                }else{
                                    showNotification({
                                        message: "You cannot",
                                        type: "error",
                                        autoClose: true,
                                        duration: 3
                                    });
                                }
                                a++;
                            }
                        });
                    },
                    // Save the setted columns to the database
                    saveSetofCols : function(element){
                        $(element).live("click", function(){
                            numCols = $("#numCols").val(); // input number of columns you want
                            setCols = $(":text[name='setofCols[]']").map(function(){
                                return $(this).val();
                            }).get().join(',');
                            // Determine if all inputs created in columns has all values
                            emptyInputs = $(this).parent().find('input[id="setofCols"]').filter(function() { return $(this).val() == ""; });
                            if (emptyInputs.length){
                                showNotification({
                                    message: "Please dont leave it blank.",
                                    type: "error",
                                    autoClose: true,
                                    duration: 3
                                });
                            }else{
                                //alert(setCols)
                                // Get tbform
                                action = "getFormField";
                                $.post("/ajax/administerMod", {action:action,numCols:numCols,setCols:setCols}, function(getFormField){
                                    $(".showFormField").html(getFormField);
                                });
                            }
                        });
                    },
                    enabledCol : function(elements){
                        $(elements).live("click", function(){
                            dataCounter = $(this).attr("data-enem-col");
                            $("select#selectLabel").attr("data-enem-col",dataCounter);
                        })
                    },
                    //
                    selectLabel : function(elements){
                        $(elements).live("change", function(){
                            //$("#selectLabel > option:selected").each(function(){
                            //    $(this).remove().appendTo("#list2");
                            //});
                            enemCol = $("select#selectLabel").attr("data-enem-col");
                            fieldID = $("#selectLabel option:selected").attr("data-field-id");
                            if(enemCol==""){
                                showNotification({
                                    message: "Please select one option of the field where do you want to add this.",
                                    type: "error",
                                    autoClose: true,
                                    duration: 3
                                });
                            }else{
                                if($("#insertSelectedCol_"+enemCol).val()!=""){
                                    showNotification({
                                        message: "Cannot update using this,",
                                        type: "error",
                                        autoClose: true,
                                        duration: 3
                                    });
                                }else{
                                    selectedfieldCol = $("#selectLabel option:selected").text();
                                    $("#selectLabel option:selected").remove();
                                    $("#insertSelectedCol_"+enemCol).val(selectedfieldCol);
                                    $("#insertSelectedCol_"+enemCol).attr("data-field-id",fieldID);
                                    
                                }
                            }
                            
                        });
                    },
                    removeSelectedField : function(elements){
                        $(elements).live("click", function(){
                            enemCol = $(this).attr("data-enem-col");
                            selectedfieldCol = $("#insertSelectedCol_"+enemCol).val();
                            fieldID = $("#insertSelectedCol_"+enemCol).attr("data-field-id");
                            if(selectedfieldCol==""){
                                showNotification({
                                    message: "Field is blank.",
                                    type: "error",
                                    autoClose: true,
                                    duration: 3
                                });
                            }else{
                                $("#selectLabel").append("<option data-field-id="+fieldID+">"+selectedfieldCol+"</option>");
                                $("#insertSelectedCol_"+enemCol).val(null);
                                $("#enabledCol_"+enemCol).removeAttr("checked");
                                $("#insertSelectedCol_"+enemCol).removeAttr("data-field-id");
                                $("select#selectLabel").attr("data-enem-col","");
                            }
                            
                        });
                    },
                    saveModSettings : function(elements){
                        
                        $(elements).live("click", function(){
                            a = 1;
                            b = 1;
                            companyID = $(".insertSelectedCol").attr("data-company-id");
                            // Determine if all inputs created in columns has all values
                            emptyInputs = $(this).parent().find('.insertSelectedCol').filter(function() { return $(this).val() == ""; });
                            if (emptyInputs.length){
                                showNotification({
                                    message: "Please dont leave it blank.",
                                    type: "error",
                                    autoClose: true,
                                    duration: 3
                                });
                            }else{
                                action = "saveFormField";
                                numCols = $("#numCols").val(); // number of columns
                                // column set - can set the title of the created columns
                                
                                setofCols = $(":text[name='setofCols[]']").map(function(){
                                    return   a++ + "->" +$(this).val();
                                    
                                }).get().join(",");
                                
                                //
                                dataFieldSet = $(":text[name='dataFieldSet[]']").map(function(){
                                    return b++ + "->" +$(this).attr("data-field-id");
                                }).get().join(",");
                                
                                //alert(setofCols + "\n" + dataFieldSet)
                                
                                // Save to the database
                                $.post("/ajax/administerMod", {action:action,setofCols:setofCols,dataFieldSet:dataFieldSet,companyID:companyID,numCols:numCols}, function(data){
                                    
                                        if(data=="Saved"){
                                            showNotification({
                                                message: "Created Column was successfully saved.",
                                                type: "success",
                                                autoClose: true,
                                                duration: 3
                                            });
                                        }
                                });
                            }
                            
                        });
                    }
                }
                
                $(document).ready(function(){
                    // Call Function - Plugin for this Administer Module
                    administerModule.viewMore("#loadMore"); // View More Announcement.
                    administerModule.lessMore("#lessMore"); // Limit view of the characters in Announcement.
                    //administerModule.saveNewAnnouncement(".btnAnnouncement"); // Save new annoucement.
                    administerModule.appendNumCols("#appendNumCols"); // Show created columns according to their inputed value.
                    administerModule.saveSetofCols("#saveSetofCols"); // Save created/set of columns to the database.
                    administerModule.selectLabel(".selectLabel");
                    administerModule.enabledCol(".enabledCol");
                    administerModule.removeSelectedField("#removeSelectedField");
                    administerModule.saveModSettings("#saveModSettings");
                    
                });
            </script>
            <div class="span7 pull-right">
                <span class="span12">
                    <h6>Announcement</h6>
                    <textarea class="input-large announcement"></textarea>
                    <input type="button" class="btn btnAnnouncement" value="Submit">
                </span>
            </div>
        </div>
    <legend class="userManagement" id="view_settings" style="cursor: pointer" rel="view_settings"><h5>View Settings<img src="/images/flowchart/right_arrow.png" height="20" style="float: right" /></h5></legend>
        <div class="view_settings" style="display: none">
        Number of Columns <br />
            <input type="text" class="input-mini" id="numCols" style="margin-top: 8px;"/>
            <input type="button" class="btn btn-success" value="Go" id="appendNumCols"/>
        <br />
            <div class="showCreatedCols">
                
            </div>
            <div class="showFormField">
                
            </div>
        </div>
    <legend class="userManagement" id="add_position" style="cursor: pointer" rel="add_position"><h5>Add Position<img src="/images/flowchart/right_arrow.png" height="20" style="float: right" /></h5></legend>
        <div class="add_position" style="display: none">
        <div class="span5 pull-left" style="padding-right: 30px;border-right: 1px solid #CCC">
            <h6>Position List</h6>
            <?php
                $queryPositionList = $db->query("SELECT * from tbposition WHERE company_id = '$company_id'","array");
                $numrowsPositionList = $db->query("SELECT * from tbposition WHERE company_id = '$company_id'","numrows");
            ?>
            <table class="table_data">
                 <thead>
                     <tr>
                         <th>#</th>
                         <th>Position</th>
                         <th colspan="3">Actions</th>
                     </tr>
                 </thead>
                 <tbody>
                    <?php
                    if($numrowsPositionList==0){
                    ?>
                        <tr>
                            <td colspan="5" style="text-align:center">No Record Found</td>
                        </tr>
                    <?php
                    }else{
                        $ctr =1;
                        foreach($queryPositionList as $dataPositionList){
                        ?>
                        <tr>
                            <td style="text-align:center"><?php echo $ctr++; ?></td>
                            <td style="text-align:center"><?php echo $dataPositionList['position']; ?></td>
                            <?php
                                echo statCol($dataPositionList['is_active'],$dataPositionList['id'],"position","");
                            ?>
                        </tr>
                        <?php
                        }
                    }
                    ?>
                 </tbody>
             </table>
         </div>
         <div class="span7 pull-right">
             <span class="span12">
                 <h6>Add Position</h6>
                 <textarea class="input-large txtPosition"></textarea>
             </span>
             <input type="button" class="btn btnPosition" value="Submit">
         </div>
        </div>
        <legend class="userManagement" id="updateApprover" style="cursor: pointer" rel="updateApprover"><h5>Update Approver<img src="/images/flowchart/down_arrow.png" height="20" style="float: right" /></h5></legend>
        <div class="updateApprover" style="min-height: 250px"> <!--style="display: none"-->
            <div style="float: left;width: 50%">
            <h6>Move Pending Request</h6>
            <label>Approver:</label>
            <select class="getApproverAdminSett" style="" rel="getAA">
                <option value="0">--Select Approver--</option>
                <option value="1">Department</option>
                <option value="2">Specific User</option>
            </select>
            <div style="display: none" class="getApproverChoices">
                
            </div>
            </div>
            <div style="float: right;width: 50%">
            <h6>Get Pending Request</h6>
            <label>Approver:</label>
            <select class="setapproverAdminSett" style="" rel="setAA">
                <option value="0">--Select Approver--</option>
                <option value="1">Department</option>
                <option value="2">Specific User</option>
            </select>
            <div style="display: none" class="setApproverChoices">
                
            </div>
            </div>
            </div>
            <input type="button" class="btn btnUpdateApprover" value="Move" style="margin-left: 30%">
            <input type="button" class="btn btnUpdateApproverReset" value="Reset" style="">
        </div>
</div>
</div>
<?php
$userPositionID = $auth['position_id'];
$getPosition = $db->query("SELECT * FROM tbposition where id = '$userPositionID'","row");
$getPreoviousApproved = $db->query("SELECT * FROM tbrequest r LEFT JOIN tbrequest_process rr on r.id=rr.request_id where rr.status!=0 AND (rr.approvedBy='$user_id') AND r.company_id='".$auth['company_id']."'","array");
foreach($getPreoviousApproved as $setPreoviousApproved){
    echo $setPreoviousApproved['request_id']."\n";
}

function tempGroupType($groupType){
    if($groupType=="1"){
        return "Head";
    }else if($groupType=="2"){
        return "Assistant Head";
    }
}

?>
<script>
        setActivNav("administer");
</script>